from appengine_django.models import BaseModel
from google.appengine.api import users
from google.appengine.ext import db

# Create your models here.
class Engine(BaseModel):
  domain = db.StringProperty(required=True)
  multiblog = db.BooleanProperty(default=False)

  @staticmethod
  def get(): return Engine.all().get()

class Author(BaseModel):
  name = db.StringProperty()
  user = db.UserProperty()

class Blog(BaseModel):
  engine = db.ReferenceProperty(Engine, collection_name='blogs')
  name = db.StringProperty()
  
  @staticmethod
  def get(name='default'):
    return Blog.all().filter('name =', 'default').get()
    
  @property
  def url(self):
    return Engine.get().domain
    
  @property
  def title(self):
    if not self.__dict__.has_key('title_val'): 
      self.__dict__['title_val'] = self.__get_setting('title')
    return self.title_val
    
  @property
  def subtitle(self):
    if not self.__dict__.has_key('subtitle_val'):
      self.__dict__['subtitle_val'] = self.__get_setting('subtitle')
    return self.subtitle_val
    
  def __get_setting(self, name):
    setting = self.get_setting(name)
    return setting and setting.value or None
  
  def get_setting(self, name):
    for setting in self.settings:
      if setting.name == name:
        return setting
    return None

    
  def get_entries(self, per_page=5, page=0):
    return Entry.all().order('-updated_at').fetch(per_page, offset=(page*per_page))
    
  def get_published_entries(self, per_page=5, page=0):
    return Entry.all().filter('published =', True).order('-updated_at').fetch(per_page, offset=(page*per_page))

class Setting(BaseModel):
  blog = db.ReferenceProperty(Blog, collection_name='settings')
  name = db.StringProperty()
  value = db.StringProperty()
  
  @property
  def id(self): return self.key().id()

class Category(BaseModel):
  blog = db.ReferenceProperty(Blog, collection_name='categories')
  name = db.StringProperty(required=True)
  
  def __cmp__(self, other):
    return cmp(self.id, other.id)
  
  @property
  def id(self): return self.key().id()
    
  @property
  def entries(self): return [ec.entry for ec in self.entry_categories]

class Entry(BaseModel):
  blog = db.ReferenceProperty(Blog, collection_name='entries')
  author = db.ReferenceProperty(Author, collection_name='entries')
  title = db.StringProperty()
  body = db.TextProperty()
  published = db.BooleanProperty(default=False)
  published_at = db.DateTimeProperty(auto_now_add=True)
  updated_at = db.DateTimeProperty(auto_now=True)
  
  def add_category(self, category):
    if not self.is_in_category(category):
      ec = EntryCategory(entry=self, category=category, name=category.name)
      ec.put()
    
  def remove_category(self, category):
    for ec in self.entry_categories:
      if ec.category == category:
        ec.delete()
    
  def is_in_category(self, category):
    return self.categories.count(category) > 0
    
  @property
  def id(self): return self.key().id()
    
  @property
  def categories(self): return [ec.category for ec in self.entry_categories]

class EntryCategory(BaseModel):
  entry = db.ReferenceProperty(Entry, collection_name='entry_categories')
  category = db.ReferenceProperty(Category, collection_name='entry_categories')
  name = db.StringProperty()